package com.epi.two;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author dxy
 * @version 1.0
 */
public class WebDemo {
    public static void main(String[] args) throws Exception {
//        socketDemo();
//        addressDemo();
//        uriDemo();
//        urlConnectionDemo();
//        httpClientDemo();

    }

    private static void httpClientDemo() throws IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://www.bilibili.com/"))
            .GET()
            .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }

    private static void urlConnectionDemo() {
        try {
            // 创建 URL 对象
            URL url = new URL("https://www.bilibili.com/");
            // 打开连接 or HttpURLConnection
            URLConnection connection = url.openConnection();
            // 设置请求头（可选）
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            // 输出流写请求体
            connection.setDoInput(true);

            // 获取输入流，读取网页内容
            try (BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream()))) {

                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void uriDemo() throws URISyntaxException {
        URI uri = new URI("mailto://user:pass@host:8080/path?query#fragment");
        System.out.println(uri.getScheme());
        System.out.println(uri.getUserInfo());
        System.out.println(uri.getAuthority());
        System.out.println(uri.getPath());
        System.out.println(uri.getQuery());

        URI baseUri = new URI("mailto://user:pass@host:8080/");
        System.out.println(baseUri.relativize(uri));
    }

    private static void addressDemo() throws UnknownHostException {
        InetAddress address = InetAddress.getByName("google.com");
        InetAddress[] addresses = InetAddress.getAllByName("google.com");
        System.out.println(address.getHostAddress());
        Arrays.stream(addresses).forEach(System.out::println);
    }

    private static void socketDemo() {
        try (var s = new Socket("time-a.nist.gov", 13);
             var in = new Scanner(s.getInputStream(), StandardCharsets.UTF_8)) {
            while (in.hasNextLine()) {
                System.out.println(in.nextLine());
            }
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
